﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace ZG_member
{
    public partial class AgeanalyseChart : Form
    {
        public AgeanalyseChart()
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
        }
        public void bangding() {
            BLL.consumeSolution cm = new BLL.consumeSolution();
            this.cbohuiyuandengji.DataSource = cm.GetModelList("");
            this.cbohuiyuandengji.DisplayMember = "solutionName";
            this.cbohuiyuandengji.ValueMember = "id";

            this.cbohuiyuandengji.SelectedIndex = 0;
            this.cboSex.SelectedIndex = 0;
            this.cbofuhao.SelectedIndex = 0;
        }

        public void sousuo(string tiaojian) {
            BLL.memberInfo m = new BLL.memberInfo();
            DataSet ds1;
            if (tiaojian == "")
            {
                ds1 = m.GetListSql("select datediff(year,birthDay,getdate()) as 年龄,COUNT(datediff(year,birthDay,getdate())) as 人数 from dbo.memberInfo group by datediff(year,birthDay,getdate())");

            }
            else
            {
                ds1 = m.GetListSql("select datediff(year,birthDay,getdate()) as 年龄,COUNT(datediff(year,birthDay,getdate())) as 人数 from dbo.memberInfo where "+tiaojian+" group by datediff(year,birthDay,getdate()) ");
            
            }
            decimal zonghe1 = 0;
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                zonghe1 = zonghe1 + decimal.Parse(ds1.Tables[0].Rows[i]["人数"].ToString());
            }
            List<string> nianling = new List<string>();
            List<string> renshu = new List<string>();
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                if (int.Parse(ds1.Tables[0].Rows[i]["年龄"].ToString()) >= 0 && int.Parse(ds1.Tables[0].Rows[i]["年龄"].ToString()) <= 16)
                {
                    string aa = ((100 / zonghe1) * decimal.Parse(ds1.Tables[0].Rows[i]["人数"].ToString())).ToString();
                    renshu.Add(aa);
                    nianling.Add("0-16" + "岁  比例：" + decimal.Parse(aa).ToString("F1") + "%");
                }
                if (int.Parse(ds1.Tables[0].Rows[i]["年龄"].ToString()) >= 17 && int.Parse(ds1.Tables[0].Rows[i]["年龄"].ToString()) <= 22)
                {
                    string aa = ((100 / zonghe1) * decimal.Parse(ds1.Tables[0].Rows[i]["人数"].ToString())).ToString();
                    renshu.Add(aa);
                    nianling.Add("17-22" + "岁  比例：" + decimal.Parse(aa).ToString("F1") + "%");
                }
                if (int.Parse(ds1.Tables[0].Rows[i]["年龄"].ToString()) >= 23 && int.Parse(ds1.Tables[0].Rows[i]["年龄"].ToString()) <= 30)
                {
                    string aa = ((100 / zonghe1) * decimal.Parse(ds1.Tables[0].Rows[i]["人数"].ToString())).ToString();
                    renshu.Add(aa);
                    nianling.Add("23-30" + "岁  比例：" + decimal.Parse(aa).ToString("F1") + "%");
                }
                if (int.Parse(ds1.Tables[0].Rows[i]["年龄"].ToString()) >= 31 && int.Parse(ds1.Tables[0].Rows[i]["年龄"].ToString()) <= 40)
                {
                    string aa = ((100 / zonghe1) * decimal.Parse(ds1.Tables[0].Rows[i]["人数"].ToString())).ToString();
                    renshu.Add(aa);
                    nianling.Add("31-40" + "岁  比例：" + decimal.Parse(aa).ToString("F1") + "%");
                }
                if (int.Parse(ds1.Tables[0].Rows[i]["年龄"].ToString()) >= 41 && int.Parse(ds1.Tables[0].Rows[i]["年龄"].ToString()) <= 50)
                {
                    string aa = ((100 / zonghe1) * decimal.Parse(ds1.Tables[0].Rows[i]["人数"].ToString())).ToString();
                    renshu.Add(aa);
                    nianling.Add("41-50" + "岁  比例：" + decimal.Parse(aa).ToString("F1") + "%");
                }
                if (int.Parse(ds1.Tables[0].Rows[i]["年龄"].ToString()) >= 51)
                {
                    string aa = ((100 / zonghe1) * decimal.Parse(ds1.Tables[0].Rows[i]["人数"].ToString())).ToString();
                    renshu.Add(aa);
                    nianling.Add("50" + "岁以上  比例：" + decimal.Parse(aa).ToString("F1") + "%");
                }
            }
            chart1.Series["Series1"]["PieLabelStyle"] = "Outside";
            chart1.Series["Series1"]["PieLinelColor"] = "Red";
            chart1.Series[0].Points.DataBindXY(nianling, renshu);
            chart1.DataBind();






            DataSet ds;
            if (tiaojian == "")
	        {
                ds = m.GetListSql("select month(birthDay) as 月, count(month(birthDay)) as 数量 from dbo.memberInfo group by month(birthDay)");

            }
            else
            {
                ds = m.GetListSql("select month(birthDay) as 月, count(month(birthDay)) as 数量 from dbo.memberInfo where " + tiaojian + " group by month(birthDay)");

            }


            

            decimal zonghe = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                zonghe = zonghe + decimal.Parse(ds.Tables[0].Rows[i]["数量"].ToString());
            }
            List<string> name = new List<string>();
            List<string> bili = new List<string>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {


                string aa = ((100 / zonghe) * decimal.Parse(ds.Tables[0].Rows[i]["数量"].ToString())).ToString();

                bili.Add(aa);
                name.Add(ds.Tables[0].Rows[i]["月"].ToString() + "月份 比例：" + decimal.Parse(aa).ToString("F1") + "%");

            }
            chart2.Series["Series1"]["PieLabelStyle"] = "Outside";
            chart2.Series["Series1"]["PieLinelColor"] = "Red";
            chart2.Series[0].Points.DataBindXY(name, bili);
            chart2.DataBind();
        
        }

        private void AgeanalyseChart_Load(object sender, EventArgs e)
        {

            this.timer1.Start();
            bangding();
            sousuo("");
           
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;

        private void AgeanalyseChart_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "";
            if (this.cboSex.Text != "全部")
            {
                sql += " sex = ";
                sql += this.cboSex.Text == "男" ? 1 : 0;
                sql += " and ";
            }
            sql += "cardType = ";
            sql += this.cbohuiyuandengji.SelectedValue;
            sql += "   ";
            if (this.txtxiaofeijine.Text != "")
            {
                sql += " and consume  " + this.cbofuhao.Text +"  ";
                sql += this.txtxiaofeijine.Text;
            }

           


            sousuo(sql);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sousuo("");
        }

        private void txtxiaofeijine_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                if (e.KeyChar == '.')
                {
                    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                        e.Handled = true;
                }
                else
                    e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        
    }
}
